home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / casm / au116-as.exe / REDATE.CPP < prev    next >
C/C++ Source or Header  |  1994-11-15  |  9KB  |  372 lines

  1. // REDATE.CPP                                 1          1    6666
  2. // Dave Harris                                11         11   6
  3. // Compiled using Borland C++ ver 3.1       1 1        1 1   6666
  4. // 03-03-94                                  1     ..   1   6   6
  5. //                                           11111 .. 11111  666
  6. ////////////////////////////////////////////////////////////////////////
  7.  
  8. /* Need to better organize this so that the from_outside flag is not
  9.    necessary */
  10.  
  11. #include "au.hpp"
  12. #include <time.h>
  13.  
  14. #define PROGRAM "REDATE"  // Name of module
  15. /*********************************************************************/
  16.  
  17. typedef struct
  18. {
  19.     int  num_days;             // Number of days to let age before processing
  20.     char change_time;         // change time as well as date
  21.     char allow_future;
  22.     DATE maxd;
  23.     int  max_hour, max_min, max_sec;
  24.     date datep;
  25.     BYTE from_outside;
  26. } REDATE_INFO;
  27.  
  28. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  29. static void rebuild_date(AU *au, ARC_HANDLE *arc_handle, char *file_name)
  30. {
  31.     struct ftime ftime_hold;
  32.     int different;
  33.     DATE date;
  34.     REDATE_INFO *in = (REDATE_INFO *)au->info;
  35.  
  36.     if (!in->from_outside)
  37.         au_printf(au, "@?1%-*s@?H", FILE_SIZE+2, file_name); // display file name
  38.  
  39.     if (in->maxd.year == -1)
  40.     {
  41.         add_to_bad_list(au, au->source_directory, file_name);
  42.         return;
  43.     }
  44.     if (in->maxd.year > in->datep.da_year ||
  45.         (in->maxd.year == in->datep.da_year &&
  46.          (in->maxd.month > in->datep.da_mon || (in->maxd.month == in->datep.da_mon &&
  47.           in->maxd.day > in->datep.da_day)
  48.        )))
  49.     {
  50.         if (in->allow_future == OFF)
  51.         {
  52.             add_to_bad_list(au, au->source_directory, file_name, 0, 3);
  53.             in->maxd.year  = in->datep.da_year;
  54.             in->maxd.month = in->datep.da_mon;
  55.             in->maxd.day   = in->datep.da_day;
  56.         }
  57.     }
  58.     arc_handle->get_time(&ftime_hold);      /* so we can keep the time the same */
  59.     different = ftime_hold.ft_day    != in->maxd.day ||
  60.                 ftime_hold.ft_month != in->maxd.month ||
  61.                 ftime_hold.ft_year    != in->maxd.year-1980;
  62.     if (in->change_time == ON)
  63.     {
  64.         different = different || ftime_hold.ft_hour != in->max_hour  ||
  65.                                  ftime_hold.ft_min    != in->max_min     ||
  66.                                  ftime_hold.ft_tsec != in->max_sec/2;
  67.     }
  68.  
  69.     ftime_hold.ft_day    = in->maxd.day;
  70.     ftime_hold.ft_month = in->maxd.month;
  71.     ftime_hold.ft_year    = in->maxd.year-1980;
  72.     if (in->change_time == ON)
  73.     {
  74.         ftime_hold.ft_hour = in->max_hour;
  75.         ftime_hold.ft_min  = in->max_min;
  76.         ftime_hold.ft_tsec = in->max_sec / 2;
  77.     }
  78.     if (!au->simulate)
  79.     {
  80.         if (different)
  81.         {
  82.             arc_handle->set_time(&ftime_hold);
  83.             if (!in->from_outside)
  84.             {
  85.                 fix_flist(au, file_name, file_name);
  86.                 au->number_changed++;
  87.             }
  88.         }
  89.     }
  90.  
  91.  // display new date
  92.  
  93.     if (!in->from_outside)
  94.     {
  95.  
  96.         au_printf(au, "%s", date_to_string(&in->maxd));
  97.         if (in->change_time == ON)
  98.         {
  99.             au_printf(au, " ");
  100.             au_printf(au, "%s", hms_to_string(in->max_hour, in->max_min, in->max_sec));
  101.         }
  102.  
  103.         if (different)
  104.         {
  105.             if (au->simulate)
  106.                 au_printf(au, " (Would be changed)");
  107.             else
  108.                 au_printf(au, " (Changed)");
  109.         }
  110.         au_printf(au, "\n");
  111.     }
  112. }
  113. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  114. static void set_max(AU *au, ARC_RECORD *r)
  115. {
  116.     REDATE_INFO *in = (REDATE_INFO *)au->info;
  117.  
  118.     if (au->date_retain == LAST)
  119.     {
  120.         if (r->date.year > in->maxd.year)
  121.             goto assign;
  122.  
  123.         if (r->date.year == in->maxd.year)
  124.         {
  125.             if (r->date.month > in->maxd.month)
  126.                 goto assign;
  127.             if (r->date.month == in->maxd.month)
  128.             {
  129.                 if ((long)r->date.day*24*60*60 + (long)r->hour*60*60 +
  130.                           (long)r->min*60 + (long)r->sec >
  131.                     (long)in->maxd.day*24*60*60 + (long)in->max_hour*60*60 +
  132.                           (long)in->max_min*60 + (long)in->max_sec)
  133.                 {
  134.                     goto assign;
  135.                 }
  136.             }
  137.         }
  138.     }
  139.     else
  140.     {
  141.         if (r->date.year < in->maxd.year)
  142.             goto assign;
  143.  
  144.         if (r->date.year == in->maxd.year)
  145.         {
  146.             if (r->date.month < in->maxd.month)
  147.                 goto assign;
  148.             if (r->date.month == in->maxd.month)
  149.             {
  150.                 if ((long)r->date.day*24*60*60 + (long)r->hour*60*60 + (long)r->min*60 +
  151.                           (long)r->sec <
  152.                     (long)in->maxd.day*24*60*60 + (long)in->max_hour*60*60 + (long)in->max_min*60 +
  153.                           (long)in->max_sec)
  154.                 {
  155.                     goto assign;
  156.                 }
  157.             }
  158.         }
  159.     }
  160.     return;
  161.  
  162.  assign:
  163.     in->maxd.year  = r->date.year;
  164.     in->maxd.month = r->date.month;
  165.     in->maxd.day   = r->date.day;
  166.     in->max_hour   = r->hour;
  167.     in->max_min    = r->min;
  168.     in->max_sec    = r->sec;
  169.  
  170.     return;
  171. }
  172. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  173. static void redate_one(AU *au, char *file_name, ARC_HANDLE *arc_handle)
  174. {
  175.     ARC_RECORD record;
  176.     int ret_code;
  177.     REDATE_INFO *in = (REDATE_INFO *)au->info;
  178.  
  179.     if (!in->from_outside)
  180.         au->number_processed++;
  181.  
  182.     for (;;)
  183.     {
  184.         ret_code = arc_handle->get_record(au, &record);
  185.         if (ret_code == EOF)
  186.             break;
  187.         else if (ret_code == -2)
  188.         {
  189.             in->maxd.year = -1;
  190.             return;
  191.         }
  192.         else if (ret_code == -3)
  193.             return;
  194.         set_max(au, &record);
  195.     }
  196.     rebuild_date(au, arc_handle, file_name);
  197.     return;
  198. }
  199. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  200. static void init_date(AU *au)
  201. {
  202.     REDATE_INFO *in = (REDATE_INFO *)au->info;
  203.  
  204.     if (au->date_retain == LAST)
  205.     {
  206.         in->maxd.year = in->maxd.month = in->maxd.day = in->max_hour =
  207.         in->max_min = in->max_sec = 0;
  208.     }
  209.     else
  210.         in->maxd.year = 32767;
  211. }
  212. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  213. static int redate(AU *au, char *file_name)
  214. {
  215.     ARC_HANDLE arc_handle;
  216.     struct ffblk ffblk;
  217.     DATE date1, date2;
  218.     REDATE_INFO *in = (REDATE_INFO *)au->info;
  219.  
  220.     check_for_key();
  221.  
  222.     findfirst(file_name, &ffblk, 0);
  223.  
  224.     date1.year    = in->datep.da_year;
  225.     date1.month = in->datep.da_mon;
  226.     date1.day    = in->datep.da_day;
  227.     fdate_to_date(&date2, &ffblk);
  228.     if (in->num_days <= 0 || days_diff(&date2, &date1) >= in->num_days)
  229.     {
  230.         init_date(au);
  231.  
  232.         arc_handle.init(au, file_name);
  233.         if (arc_handle.type > 0)
  234.             redate_one(au, file_name, &arc_handle);
  235.         arc_handle.deinit(au);
  236.     }
  237.     return 0;
  238. }
  239. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  240. static void ReadCFGInfo(AU *au, CFG_HANDLE *cfg_handle)
  241. {
  242.     REDATE_INFO *in = (REDATE_INFO *)au->info;
  243.  
  244.     for(EVER)
  245.     {
  246.         char string[200],
  247.              string2[200],
  248.              string3[200];
  249.  
  250.         if (cfg_handle->read_line(au, string)==EOF)
  251.             break;
  252.  
  253.         split_string(string, string2);
  254.         split_string(string, string3);
  255.  
  256.         if (string2[0] == '\0')
  257.             continue;
  258.  
  259.         strcpy(au->curOpt, string2);
  260.         au->curVal = string3;
  261.         switch (toupper(string2[1]) << 8 | toupper(string2[0]))
  262.         {
  263.             case 'BE':                                          // Begin
  264.                 return;
  265.             case 'AG':
  266.                 in->num_days = atoi(string3);
  267.                 break;
  268.             case 'TI':
  269.                 in->change_time = get_value(au, OFF | ON);
  270.                 break;
  271.             case 'AL':
  272.                 in->allow_future = get_value(au, OFF | ON);
  273.                 break;
  274.             default:
  275.                 cfg_handle->invalid_option(au, string2);
  276.         }
  277.     }
  278. }
  279. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  280. static BYTE parse_comm_line(AU *au, char option, char *cur_argv,
  281.                             PARSE_TYPE type)
  282. {
  283.     REDATE_INFO *in = (REDATE_INFO *)au->info;
  284.  
  285.     switch (type)
  286.     {
  287.     case PARSE_PARAM_OPTION:
  288.         switch (option)
  289.         {
  290.         case 'X':
  291.             au->self_extracts = get_value(au, OFF | ON);
  292.             break;
  293.         case 'D':
  294.             au->date_retain = get_value(au, FIRST | LAST);
  295.             break;
  296.         case 'A':
  297.             in->num_days = atoi(cur_argv);
  298.             break;
  299.         case 'T':
  300.             in->change_time = get_value(au, OFF | ON);
  301.             break;
  302.         case 'L':
  303.             in->allow_future = get_value(au, OFF | ON);
  304.             break;
  305.         case '?':
  306.             au_standard_opt_header(au, "Redate",
  307.                "@?3-X@?Hon|off         process self eXtracts\n"
  308.                "@?3-T@?Hon|off         change Time too\n"
  309.                "@?3-L@?Hon|off         aLlow future dates\n"
  310.                "@?3-D@?Hlast|first     Date as\n"
  311.                "@?3-A@?Hn              let Age n days before processing\n");
  312.             exit(0);
  313.         default:
  314.             au_invalid_option(au, PROGRAM, option);
  315.         }
  316.         return TRUE;
  317.     }
  318.     return FALSE;
  319. }
  320. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  321. int redate_file(AU *au, char *file_name)
  322. {
  323.     REDATE_INFO *in;
  324.     void *old_info;
  325.  
  326.     in = new REDATE_INFO;
  327.     memset(in, '\0', sizeof(REDATE_INFO));
  328.  
  329.     old_info = au->info;
  330.     au->info = in;
  331.     in->change_time  = ON;
  332.     in->allow_future = ON;
  333.     in->from_outside = TRUE;
  334.  
  335.     redate(au, file_name);
  336.     au->info = old_info;
  337.     return 0;
  338. }
  339. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  340. int main_redate(AU *au, int argc, char *argv[])
  341. {
  342.     char string[120];
  343.     HANDLE file;
  344.     REDATE_INFO *in;
  345.  
  346.     in = new REDATE_INFO;
  347.     memset(in, '\0', sizeof(REDATE_INFO));
  348.     au->info = in;
  349.     in->change_time  = OFF;
  350.     in->allow_future = OFF;
  351.  
  352.     getdate(&in->datep);                  // Current date */
  353.  
  354.     au->allow_rename = FALSE;             // no sense since unarcers not called
  355.     au->date_retain = LAST;              // as a default
  356.     ReadGlobalCFGInfo(au, au->cfg_file, PROGRAM, ReadCFGInfo);
  357.     generic_parse_comm_line(au, argc, argv, parse_comm_line);
  358.     process_files(au, redate);
  359.  
  360.     if (!au->no_extra)
  361.         au_printf_c(au, 15, "\nFiles Processed = %d\n", au->number_processed);
  362.  
  363.     if (au->number_processed > 0)
  364.     {
  365.         if (!au->no_extra)
  366.             au_printf_c(au, 15, "Files Modified  = %d\n", au->number_changed);
  367.     }
  368.  
  369.     return 0;
  370. }
  371.  
  372.